package com.cskaoyan.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @author stone
 * @date 2022/06/16 17:04
 */
@Component
public class LifeCycleBean implements BeanNameAware, BeanFactoryAware, ApplicationContextAware,
        InitializingBean, DisposableBean {
    public LifeCycleBean() {
        System.out.println("生命周期的开始，bean的实例化");
    }

    String username;
    User user;

    @Value("songge")
    public void setUsername(String username) {
        System.out.println("set方法赋值");
        this.username = username;
    }

    @Autowired
    public void setUser(User user) {
        System.out.println("setUser方法");
        this.user = user;
    }

    String beanName;
    BeanFactory beanFactory;
    ApplicationContext applicationContext;

    @Override
    public void setBeanName(String beanName) {
        System.out.println("setBeanName:" + beanName);
        this.beanName = beanName;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("setBeanFactory:" + beanFactory);
        this.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("setApplicationContext:" + applicationContext);
        this.applicationContext = applicationContext;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean提供的init方法");
    }

    // 自定义init方法，方法名自己定义
    @PostConstruct
    public void init() {
        System.out.println("自定义init");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("DisposableBean提供的destroy");
    }

    @PreDestroy
    public void customDestroy() {
        System.out.println("自定义destroy");
    }
}
